added Feb 2001 SDK
[windows-sources.git] / shared source / sscli20 / jscript / engine / mathobject.cs
blob0d769b0e1624fe96b5a56d11d4c6fdbdbe4699f5
1 // ==++==
2 //
3 //
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
5 //
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
10 //
11 // You must not remove this notice, or any other, from this software.
12 //
13 //
14 // ==--==
16 namespace Microsoft.JScript {
18 using System;
20 public class MathObject : JSObject{
21 public const double E = 2.7182818284590452354;
22 public const double LN10 = 2.302585092994046;
23 public const double LN2 = 0.6931471805599453;
24 public const double LOG2E = 1.4426950408889634;
25 public const double LOG10E = 0.4342944819032518;
26 public const double PI = 3.14159265358979323846;
27 public const double SQRT1_2 = 0.7071067811865476;
28 public const double SQRT2 = 1.4142135623730951;
30 private static readonly System.Random internalRandom = new System.Random();
31 internal static MathObject ob = null;
33 internal MathObject(ScriptObject parent)
34 : base(parent) {
37 [JSFunctionAttribute(0, JSBuiltin.Math_abs)]
38 public static double abs(double d){
39 if (d < 0)
40 return -d;
41 else if (d > 0)
42 return d;
43 else if (d == d)
44 return 0.0;
45 else
46 return d;
49 [JSFunctionAttribute(0, JSBuiltin.Math_acos)]
50 public static double acos(double x){
51 return System.Math.Acos(x);
54 [JSFunctionAttribute(0, JSBuiltin.Math_asin)]
55 public static double asin(double x){
56 return System.Math.Asin(x);
59 [JSFunctionAttribute(0, JSBuiltin.Math_atan)]
60 public static double atan(double x){
61 return System.Math.Atan(x);
64 [JSFunctionAttribute(0, JSBuiltin.Math_atan2)]
65 public static double atan2(double dy, double dx){
66 return System.Math.Atan2(dy, dx);
69 [JSFunctionAttribute(0, JSBuiltin.Math_ceil)]
70 public static double ceil(double x){
71 return System.Math.Ceiling(x);
74 private static double Compare(double x, double y){
75 if (x != 0 || y != 0)
76 if (x == y) //x and y could be infinities, in which case - will not return 0.
77 return 0;
78 else
79 return x - y;
80 double x1 = 1 / x; //will be < 0 if x == -0
81 double y1 = 1 / y;
82 if (x1 < 0){
83 return y1 < 0 ? 0 : -1;
84 }else if (y1 < 0)
85 return 1;
86 else
87 return 0;
90 [JSFunctionAttribute(0, JSBuiltin.Math_cos)]
91 public static double cos(double x){
92 return System.Math.Cos(x);
95 [JSFunctionAttribute(0, JSBuiltin.Math_exp)]
96 public static double exp(double x){
97 return System.Math.Exp(x);
100 [JSFunctionAttribute(0, JSBuiltin.Math_floor)]
101 public static double floor(double x){
102 return System.Math.Floor(x);
105 internal override String GetClassName(){
106 return "Math";
109 [JSFunctionAttribute(0, JSBuiltin.Math_log)]
110 public static double log(double x){
111 return System.Math.Log(x);
114 [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_max)]
115 public static double max(Object x, Object y, params Object[] args){
116 if (x is Missing)
117 return Double.NegativeInfinity;
118 double dx = Convert.ToNumber(x);
119 if (y is Missing)
120 return dx;
121 double dy = Convert.ToNumber(y);
122 double result = MathObject.Compare(dx, dy);
123 if (result != result) return result;
124 double lhMax = dx;
125 if (result < 0) lhMax = dy;
126 if (args.Length == 0) return lhMax;
127 return MathObject.maxv(lhMax, args, 0);
130 private static double maxv(double lhMax, Object[] args, int start){
131 if (args.Length == start)
132 return lhMax;
133 double head = Convert.ToNumber(args[start]);
134 double result = MathObject.Compare(lhMax, head);
135 if (result != result) return result;
136 if (result > 0) head = lhMax;
137 return MathObject.maxv(head, args, start+1);
140 [JSFunctionAttribute(JSFunctionAttributeEnum.HasVarArgs, JSBuiltin.Math_min)]
141 public static double min(Object x, Object y, params Object[] args){
142 if (x is Missing)
143 return Double.PositiveInfinity;
144 double dx = Convert.ToNumber(x);
145 if (y is Missing)
146 return dx;
147 double dy = Convert.ToNumber(y);
148 double result = MathObject.Compare(dx, dy);
149 if (result != result) return result;
150 double lhMin = dx;
151 if (result > 0) lhMin = dy;
152 if (args.Length == 0) return lhMin;
153 return MathObject.minv(lhMin, args, 0);
156 private static double minv(double lhMin, Object[] args, int start){
157 if (args.Length == start)
158 return lhMin;
159 double head = Convert.ToNumber(args[start]);
160 double result = MathObject.Compare(lhMin, head);
161 if (result != result) return result;
162 if (result < 0) head = lhMin;
163 return MathObject.minv(head, args, start+1);
166 [JSFunctionAttribute(0, JSBuiltin.Math_pow)]
167 public static double pow(double dx, double dy){
168 if (dy == 0)
169 return 1.0;
170 if ((dx == 1.0 || dx == -1.0) && (dy == Double.PositiveInfinity || dy == Double.NegativeInfinity))
171 return Double.NaN;
172 if (Double.IsNaN(dy))
173 return Double.NaN;
175 // If dy is an odd integer, return -0. This case is inconsistent between x86 and amd64.
176 if (dx == Double.NegativeInfinity && dy < 0.0) {
177 if (Math.IEEERemainder((-dy)+1.0, 2.0) == 0.0)
178 return -0.0;
181 try{
182 return System.Math.Pow(dx, dy);
183 }catch{
184 if (dx != dx || dy != dy)
185 return Double.NaN;
186 if (dx == 0.0)
187 if (dy < 0.0){
188 if ((double)(long)dy == dy && ((long)(-dy)) % 2 > 0){
189 double f = 1.0 / dx;
190 if (!(f >= 0.0))
191 return Double.NegativeInfinity;
193 return Double.PositiveInfinity;
195 return Double.NaN;
199 [JSFunctionAttribute(0, JSBuiltin.Math_random)]
200 public static double random(){
201 return MathObject.internalRandom.NextDouble();
204 [JSFunctionAttribute(0, JSBuiltin.Math_round)]
205 public static double round(double d){
206 if (d == 0)
207 return d;
208 else
209 return System.Math.Floor(d+0.5);
212 [JSFunctionAttribute(0, JSBuiltin.Math_sin)]
213 public static double sin(double x){
214 return System.Math.Sin(x);
217 [JSFunctionAttribute(0, JSBuiltin.Math_sqrt)]
218 public static double sqrt(double x){
219 return System.Math.Sqrt(x);
222 [JSFunctionAttribute(0, JSBuiltin.Math_tan)]
223 public static double tan(double x){
224 return System.Math.Tan(x);